import { NextRequest, NextResponse } from "next/server";

// 模拟一个简单单用户
let mockUserIdCounter = 11;
const mockDatabase: User[] = [];

interface User {
  id: number;
  name: string;
  email: string;
}
export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    if (!body.name || !body.email) {
      return NextResponse.json({ message: "缺少参数" }, { status: 400 });
    }

    const mockUser: User = {
      id: mockUserIdCounter++,
      name: body.name,
      email: body.email,
    };

    mockDatabase.push(mockUser);
    console.log("创建用户成功:", mockUser);
    return NextResponse.json(mockUser, { status: 201 });
  } catch (error) {
    console.error("API 路由错误:", error);
    return NextResponse.json({ message: "服务器内部错误" }, { status: 500 });
  }
}

export async function GET(request: NextRequest) {
  return NextResponse.json(mockDatabase, { status: 200 });
}
